<template>
  <div class="navbar">
    <hamburger id="hamburger-container" :is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar" />
    <!-- <breadcrumb id="breadcrumb-container" class="breadcrumb-container" /> -->
    <div class="right-menu">
      <!-- <template v-if="device !== 'mobile'">
        <search id="header-search" class="right-menu-item" />

        <screenfull id="screenfull" class="right-menu-item hover-effect" />
      </template> -->
      <el-popover ref="mergePopover" :width="350" trigger="hover">
        <div slot="reference">
          <el-badge v-if="isNotice[0]" :value="isNotice[1]" class="item mt20">
            <img src="@/assets/notice.png" width="25" height="25" alt="">
          </el-badge>
          <img v-else src="@/assets/notice.png" width="25" height="25" class="mt20">
        </div>
        <div v-if="tableData.length > 0" class="message-list">
          <ul v-infinite-scroll="getNewNoticeData" class="infinite-list" style="overflow: auto">
            <li v-for="(item, index) in tableData" :key="index" class="infinite-list-item flex flex-row" @click="jumpNewTodo(item)">
              <img alt class="messageimg" src="@/assets/noBody.png">
              <div class="flex-auto pl10 pr10 font15">
                <div>{{ item.logTitle }}</div>
                <div class="font14 mt5">{{ item.createTime }}</div>
              </div>
              <div v-if="item.operateStatus == 0" class="pointer" />
              <div v-else class="pointer-no" />
            </li>
          </ul>
        </div>
        <el-empty v-else description="暂无消息" />
        <p v-if="noMore" class="text-center font14">没有更多数据了...</p>
      </el-popover>
      <!-- <el-dropdown
        class="avatar-container right-menu-item hover-effect ml20"
        trigger="click"
      >
        <div class="avatar-wrapper">
          <img
            v-if="avatar != ''"
            :src="avatar + '?imageView2/1/w/80/h/80'"
            class="user-avatar"
            alt=""
          />
          <img
            v-else
            src="@/assets/default_avatar.jpeg"
            class="user-avatar"
            alt=""
          />
          <span style="font-size: 16px;" class="pl10">欢迎您，{{ name }}</span>
          <i class="el-icon-caret-bottom" />
        </div>

        <el-dropdown-menu slot="dropdown">
          <router-link to="/profile/index">
            <el-dropdown-item>个人中心</el-dropdown-item>
          </router-link>
          <el-dropdown-item divided>
            <span style="display:block;" @click="logout">退出登录</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown> -->
      <div class="avatar-wrapper cursor-p mr20 ml20">
        <img v-if="avatar != ''" :src="avatar + '?imageView2/1/w/80/h/80'" class="user-avatar" alt="">
        <img v-else src="@/assets/default_avatar.jpeg" class="user-avatar" alt="">
        <span style="font-size: 16px;" class="pl10">欢迎您，{{ name }}</span>
      </div>
<!--      <div class="ml10 font16 line-height-20 cursor-p password mr20" @click="goPassword">
        <i class="el-icon-edit" />修改密码
      </div>-->
      <div class="avatar-wrapper cursor-p pr20" @click="backToNav">
        <i class="el-icon-refresh-left" style="font-size: 18px;font-weight:600" />
        <span style="font-size: 16px;">返回医务一网通办</span>
      </div>
      <div class="avatar-wrapper cursor-p pr20" @click="logout">
        <i class="el-icon-unlock" style="font-size: 16px;font-weight:600" />
        <span style="font-size: 16px;">退出登录</span>
      </div>
    </div>
    <Password ref="passwordRef" />
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import Screenfull from '@/components/Screenfull'
import Search from '@/components/HeaderSearch'
import { noticeList, readNotice,newNoticeList,newReadNotice } from '@/api/process/notice'
import Password from '@/components/password'
export default {
  components: {
    Breadcrumb,
    Hamburger,
    Screenfull,
    Search,
    Password
  },
  data() {
    return {
      tableData: [],
      noMore: false,
      pageIndex: 1,
      pageSize: 9999
    }
  },
  mounted() {
    // this.getNoticeData()
    this.getNewNoticeData()
  },
  computed: {
    ...mapGetters(['sidebar', 'avatar', 'device', 'name']),
    isNotice() {
      let noticeState = false
      const result = []
      let count = 0
      this.tableData.forEach(item => {
        if (item.operateStatus == 0) {
          noticeState = true
          count = count + 1
        }
      })
      result[0] = noticeState
      result[1] = count
      return result
    }
  },
  methods: {
    goPassword() {
      this.$refs.passwordRef.open()
    },
    async getNoticeData() {
      const table = await noticeList({
        pageIndex: this.pageIndex,
        pageSize: this.pageSize
      })
      if (table.code == 200) {
        this.tableData = [...this.tableData, ...table.data.list]
        this.pageIndex = table.data.pageIndex + 1
        this.pageSize = table.data.pageSize
        if (table.data.list.length == 0) {
          this.noMore = true
        }
      }
    },
    async getNewNoticeData() {
      const table = await newNoticeList({
        pageSize: 1
      })
      // console.log(1111,table)
      // this.tableData = [...this.tableData, ...table.records]
      this.tableData = table.records
      // this.pageIndex = table.current + 1
      // this.pageSize = table.size
      if (table.records.length == 0) {
        this.noMore = true
      }
    },
    async jumpTodo(item) {
      const res = await readNotice({ id: item.id })
      if (res.code == 200) {
        // this.pageIndex = 1
        this.tableData = []
        this.getNoticeData()
        if (item.process == 2) {
          this.$router.push('/annual/todo')
        } else {
          this.$router.push('/project/upcoming')
        }
      }
    },
    async jumpNewTodo(item) {
      // this.pageIndex = 1
      const res = await newReadNotice({ id: item.id })
      this.tableData = []
      this.getNewNoticeData()
      if(item.relatedType==='审批'){
        this.$router.push('/xjsxxm/apply/pendApproval')
      } else {
        this.$router.push('/upms/sysuser')
      }
      // const res = await readNotice({ id: item.id })
      // if (res.code == 200) {
      //   this.pageIndex = 1
      //   this.tableData = []
      //   this.getNoticeData()
      //   if (item.process == 2) {
      //     this.$router.push('/annual/todo')
      //   } else {
      //     this.$router.push('/project/upcoming')
      //   }
      // }
    },
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    backToNav() {
      this.$router.push('/')
    },
    async logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('user/LogOut').then(() => {
          location.reload()
        })
      })
    }
  }
}

</script>
<style lang="scss" scoped>
.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background 0.3s;
    -webkit-tap-highlight-color: transparent;

    &:hover {
      background: rgba(0, 0, 0, 0.025);
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }

  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;
    display: flex;
    flex-direction: row;
    align-items: center;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background 0.3s;

        &:hover {
          background: rgba(0, 0, 0, 0.025);
        }
      }
    }

    .avatar-container {
      margin-right: 20px;

    }
  }
}

.avatar-wrapper {
  display: flex;
  flex-direction: row;
  align-items: center;

  .user-avatar {
    cursor: pointer;
    width: 28px;
    height: 28px;
    border-radius: 50%;
  }

  .el-icon-caret-bottom {
    cursor: pointer;

    font-size: 12px;
  }
}

.infinite-list {
  height: 460px;
  padding: 0;
  margin: 0;
  list-style: none;
}

.messageimg {
  width: 32px;
  height: 32px;
  align-self: flex-start;
}

.pointer {
  width: 8px;
  height: 8px;
  background: #dd2f2b;
  border-radius: 50%;
}

.pointer-no {
  width: 8px;
  height: 8px;
}

.infinite-list .infinite-list-item {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 10px;
  color: #606060;
  cursor: pointer;
  border-bottom: 1px solid #f0f0f0;
  padding: 10px 0;
}

.infinite-list .infinite-list-item+.list-item {
  margin-top: 10px;
}

</style>
